<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\CustomerExporter;
use App\Admin\Extensions\Transfer;
use App\model\Area;
use App\model\Coustomer;
use App\Http\Controllers\Controller;
use App\model\CoustomerDemand;
use App\model\CustomerArea;
use App\model\CustomerLevel;
use App\model\CustomerSign;
use App\model\Industry;
use App\model\Project;
use App\model\Source;
use App\User;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;
use Illuminate\Http\Request;

class CoustomerController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('客户管理')
            ->description('')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }
    /**
     * Edit interface.
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Coustomer);
        $grid->model()->with('follow')->orderBy('customer_id','desc');
        $grid->customer_id('客户id');
        $grid->name('客户名称');
        $grid->mobile('联系方式')->expand(function ($model) {
            return new Table(['联系方式','内容'],[['手机号',$model->mobile],['微信号',$model->wechat],['固话',$model->tel],['公司地址',$model->address]]);
        });;
        $grid->column('user.name','业务员');
        $grid->column('writer_id','推广人')->display(function($value){
            return User::find($value)->name ?? '无推广人';
        });
        $grid->company_name('企业名称');
        $grid->status('状态')->select(Coustomer::$statusMap);
        $grid->follow_status('跟进状态')->display(function($value){
            return Coustomer::$followStatusMap[$value] ?? '';
        });
        $grid->genjin('跟进记录')->display(function($value){
            return "<a href='/admin/follow?customer_id=$this->customer_id' target='_blank'>跟进记录</a>";
        });
        $grid->exporter(new CustomerExporter());
        $grid->column('industry.name','行业名称');
        $grid->column('project.name','项目名称');
        $grid->column('source.name','渠道来源');
        $grid->column('area.name','需求面积');
        $grid->column('demand.name','需求类型');
        $grid->column('level.name','等级名称');

        $grid->city1('省份');
        $grid->city2('城市');

        $grid->created_at('创建时间');
        $grid->filter(function(Grid\Filter $filter) {
            $filter->equal('mobile','手机账号');
            $filter->disableIdFilter();
            $filter->where(function($query){
                $query->whereHas('user', function ($query) {
                    $query->where('name', 'like', "%{$this->input}%");
                });
            },'业务员名称' );
            $filter->where(function($query){
                $query->whereHas('level', function ($query) {
                    $query->where('customer_level_id', '=', $this->input);
                });
            },'等级' );
           $filter->in('project_id','所属项目')->multipleSelect(Project::getSelectOptions());
            $filter->like('name','客户名称');
        });
        $grid->tools(function (Grid\Tools $tools){
            $tools->batch(function (Grid\Tools\BatchActions $actions){
                    $actions->disableDelete();
                    $actions->add('转移客户',new Transfer(1));
            });
        });
        $grid->actions(function(Grid\Displayers\Actions $actions){
            $actions->disableView();
        });
        return $grid;
    }
    public function transfer(Request $request){
      if (empty($request->get('ids'))) {
          return response()->json(
              ['code'=>1]
          );
      }
        foreach (Coustomer::find($request->get('ids')) as $post) {
            $post->user_id = $request->get('user_id');
            $post->save();
        }
        return response()->json(
            ['code'=>0]
        );
    }
    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Coustomer::findOrFail($id));

        $show->customer_id('Customer id');
        $show->mobile('Mobile');
        $show->company_name('Company name');
        $show->industry_id('Industry id');
        $show->project_id('Project id');
        $show->source_id('Source id');
        $show->created_at('Created at');
        $show->updated_at('Updated at');
        $show->name('Name');
        $show->user_id('User id');

        return $show;
    }
    public function getUser(Request $request){
        $user=User::find($request->id);
        if ($user){
            return response()->json(
                [
                    'code'=>1,
                    'name'=>$user->name
                    ]
            );
        }else{
            return response()->json(
                [
                    'code'=>0,
                ]
            );
        }
    }
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Coustomer);
        $form->tab('基本信息',function(Form $form){
            $form->text('name', '姓名');
            $form->multipleSelect('sign','客户类型')->options(CustomerSign::getSelectOptions());
            $form->select('customer_level_id','客户等级')->options(CustomerLevel::getSelectOptions());
            $form->select('status','状态')->options(Coustomer::$statusMap);
            $form->select('follow_status','状态')->options(Coustomer::$followStatusMap);
        })->tab('联系方式',function(Form $form){
            $form->mobile('mobile', '手机号');
            $form->text('Wechat','微信号');
            $form->text('tel','固话');
        })->tab('公司信息',function (Form $form){
        $form->text('company_name','公司名称');
        $form->text('company_address','公司地址');
    })->tab('客户需求',function(Form $form){
        $form->select('demand_id','需求类型')->options(CoustomerDemand::getSelectOptions());
        $form->select('source_id','来源渠道')->options(Source::getSelectOptions());
        $form->select('industry_id','行业')->options(Industry::getSelectOptions());
        $form->select('project_id','所属项目')->options(Project::getSelectOptions());
        $form->select('area_id','需求面积')->options(CustomerArea::getSelectOptions());
    });
        return $form;
    }
}
